package com.north.light.androidutils.canvas

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.north.light.androidutils.R
import com.north.light.androidutils.canvas.ability.AbilityChart
import com.north.light.androidutils.canvas.ability.AbilityTxInfo
import com.north.light.androidutils.canvas.ability.AbilityXYInfo
import com.north.light.androidutils.canvas.circle.CircleBuilder
import com.north.light.androidutils.canvas.circle.CircleChart
import com.north.light.androidutils.canvas.circle.CircleInfo
import com.north.light.androidutils.canvas.examprogress.ExamProgressBarInfo
import com.north.light.androidutils.canvas.examprogress.ExamProgressView
import com.north.light.androidutils.canvas.linear.LinearBuilder
import com.north.light.androidutils.canvas.linear.LinearChart
import com.north.light.androidutils.canvas.linear.LinearInfo
import com.north.light.androidutils.canvas.progressbar.ProgressBarInfo
import com.north.light.androidutils.canvas.progressbar.ProgressBarView
import com.north.light.androidutils.canvas.selectionprogressbar.*
import com.north.light.androidutils.log.LogUtil


/**
 * 图表绘制activity
 * */
class CanvasActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_canvas)
        //饼状图
        val circleChart = findViewById<CircleChart>(R.id.activity_canvas_circle)
        val circleData = mutableListOf<CircleInfo>(
            CircleInfo().apply {
                this.color = R.color.colorPrimary
                this.percent = 90f
            },
            CircleInfo().apply {
                this.color = R.color.blue_light
                this.percent = 9f
            })
        circleChart.setData(
            CircleBuilder().apply {
                this.dataList = circleData
                this.bottomColor = R.color.colorAccent
            })
        //直方图
        val linearChart = findViewById<LinearChart>(R.id.activity_canvas_linear)
        val linearData = mutableListOf<LinearInfo>(
            LinearInfo().apply {
                this.color = R.color.colorPrimary
                this.yData = 90f
            },
            LinearInfo().apply {
                this.color = R.color.blue_light
                this.yData = 9f
            },
            LinearInfo().apply {
                this.color = R.color.color_F06091
                this.yData = 19f
            },
            LinearInfo().apply {
                this.color = R.color.color_F06091
                this.yData = 29f
            },
            LinearInfo().apply {
                this.color = R.color.blue_light
                this.yData = 9f
            },
            LinearInfo().apply {
                this.color = R.color.blue_light
                this.yData = 9f
            },
            LinearInfo().apply {
                this.color = R.color.blue_light
                this.yData = 9f
            })
        linearChart.setData(
            LinearBuilder().apply {
                this.dataList = linearData
                this.dataList = linearData
            })


        //自定义进度条
        val progress: ProgressBarView = findViewById(R.id.pvProgress)
        progress.setProgress(ProgressBarInfo())


        //自定义水平进度条------------------------------------------------------------------------------

        //默认状态
        val progress3: SelectionProgressHorizontalBarView = findViewById(R.id.pvProgress3)
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            progress3.setProgress(SelectionProgressBarInfo().apply {
                this.status = 0
                this.progress = 20
            }, true)
        }, 1200)

        //没有选中--进度为10
        val progress4: SelectionProgressHorizontalBarView = findViewById(R.id.pvProgress4)
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            progress4.setProgress(SelectionProgressBarInfo().apply {
                this.status = 1
                this.progress = 60
            }, true)
        }, 1600)

        //选中--进度为50
        val progress2: SelectionProgressHorizontalBarView = findViewById(R.id.pvProgress2)
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            progress2.setProgress(SelectionProgressBarInfo().apply {
                this.status = 2
                this.selectTouchX = 80f
                this.selectTouchY = 100f
                this.progress = 2
            }, true)
        }, 2000)
        progress2.setSelectionProgressBarListener(object : SelectionProgressBarListener {
            override fun progressChange(progress: Int, max: Int) {
            }

            override fun click(v: View?, x: Float, y: Float) {
                progress2.setProgress(SelectionProgressBarInfo().apply {
                    this.status = 2
                    this.selectTouchX = x
                    this.selectTouchY = y
                    this.progress = 98
                })
            }
        })


        //自定义垂直进度条------------------------------------------------------------------------------

        //默认状态
        val progress11: SelectionProgressVerticalBarView = findViewById(R.id.pvProgress11)
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            progress11.setProgress(SelectionProgressBarInfo().apply {
                this.status = 0
                this.progress = 20
            }, true)
        }, 1200)

        //没有选中--进度为10
        val progress12: SelectionProgressVerticalBarView = findViewById(R.id.pvProgress12)
        progress12.setSelectionProgressBarListener(object : SelectionProgressBarListener{
            override fun progressChange(progress: Int, max: Int) {
                LogUtil.d("progress12 progressChange： " + progress + "max: " + max)
            }

            override fun click(v: View?, x: Float, y: Float) {

            }
        })

        val progress13: SelectionProgressVerticalBarView = findViewById(R.id.pvProgress13)


        Handler(Looper.getMainLooper()).postDelayed(Runnable {

            progress13.setProgress(SelectionProgressBarInfo().apply {
                this.status = 2
                this.selectTouchX = 80f
                this.selectTouchY = 100f
                this.progress = 20
            }, true)

            progress12.setProgress(SelectionProgressBarInfo().apply {
                this.status = 1
                this.progress = 60
            }, true)


        }, 1600)

        progress13.setSelectionProgressBarListener(object : SelectionProgressBarListener {
            override fun progressChange(progress: Int, max: Int) {
                LogUtil.d("progress13 progressChange： " + progress + "max: " + max)
            }

            override fun click(v: View?, x: Float, y: Float) {
                LogUtil.d("progress13 cus onclick")
                progress13.setProgress(SelectionProgressBarInfo().apply {
                    this.status = 2
                    this.selectTouchX = x
                    this.selectTouchY = y
                    this.progress = 98
                })
            }
        })
        progress13.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                LogUtil.d("progress13 onclick")
            }
        })


        //自定义进度条2
        val examProgress: ExamProgressView = findViewById(R.id.examProgress)
        val exap = ExamProgressBarInfo()
        exap.stepCount.add(10)
        exap.stepCount.add(40)
        exap.stepCount.add(20)
        exap.stepCount.add(60)
        exap.progress = 5
        examProgress.setProgress(exap)
        for (i in 0..100) {
            Handler().postDelayed(Runnable {
                exap.progress = Math.min((exap.progress + 1), 100)
                examProgress.setProgress(exap)
                val hard = examProgress.isHardwareAccelerated
                val progress2Hard = examProgress.isHardwareAccelerated
                LogUtil.d("ExamProgressView hard: $hard")
                LogUtil.d("ExamProgressView progress2Hard: $progress2Hard")
            }, ((100 * (i + 1)).toLong()))
        }


        //能力图表
        val abilityChart = findViewById<AbilityChart>(R.id.activity_canvas_ability)
        abilityChart.setData(mutableListOf(AbilityTxInfo().apply {
            this.title = "职场"
            this.progress = 20
        }, AbilityTxInfo().apply {
            this.title = "财富"
        }, AbilityTxInfo().apply {
            this.title = "沟通"
            this.progress = 60
        }, AbilityTxInfo().apply {
            this.title = "外型"
        }, AbilityTxInfo().apply {
            this.title = "情感"
        }), AbilityXYInfo())


        val rvContent = findViewById<SelectionDataRecyclerView>(R.id.rvData)
        rvContent.initData()

//        Handler(Looper.getMainLooper()).postDelayed(Runnable {
//            rvContent.initData()
//        }, 4000)

        //lottie
        val lottieView = findViewById<SelectionLottieView>(R.id.lottieView)
        lottieView.start("lp", "lp/refresh.json")

    }
}